<emu-clause id="await" aoid="Await">
  <h1><ins>Await</ins></h1>

  <emu-note type=editor>
    <p>The AsyncFunctionAwait abstract operation could probably be refactored in terms of this primitive.</p>
  </emu-note>

  <p>Algorithm steps that say</p>

  <emu-alg>
    1. Let _completion_ be Await(_promise_).
  </emu-alg>

  <p>mean the same thing as:</p>

  <emu-alg>
    1. Let _asyncContext_ be the running execution context.
    1. Let _promiseCapability_ be ! NewPromiseCapability(%Promise%).
    1. Perform ! Call(_promiseCapability_.[[Resolve]], *undefined*, « _promise_ »).
    1. Let _onFulfilled_ be a new built-in function object as defined in <emu-xref href="#await-fulfilled" title></emu-xref>.
    1. Let _onRejected_ be a new built-in function object as defined in <emu-xref href="#await-rejected" title></emu-xref>.
    1. Set _onFulfilled_ and _onRejected_'s [[AsyncContext]] internal slots to _asyncContext_.
    1. Let _throwawayCapability_ be NewPromiseCapability(%Promise%).
    1. Set _throwawayCapability_.[[Promise]].[[PromiseIsHandled]] to *true*.
    1. Perform ! PerformPromiseThen(_promiseCapability_.[[Promise]], _onFulfilled_, _onRejected_, _throwawayCapability_).
    1. Remove _asyncContext_ from the execution context stack and restore the execution context that is at the top of the execution context stack as the running execution context.
    1. Set the code evaluation state of _asyncContext_ such that when evaluation is resumed with a Completion _completion_, the following steps of the algorithm that invoked Await will be performed, with _completion_ available.
  </emu-alg>

  <p>where all variables in the above steps, with the exception of _completion_, are ephemeral and visible only in the steps pertaining to Await.</p>

  <emu-note>
    <p>Await can be combined with the `?` and `!` prefixes, so that for example</p>

    <emu-alg>
      1. Let _value_ be ? Await(_promise_).
    </emu-alg>

    <p>means the same thing as:</p>

    <emu-alg>
      1. Let _value_ be Await(_promise_).
      1. ReturnIfAbrupt(_value_).
    </emu-alg>
  </emu-note>

  <emu-clause id="await-fulfilled">
    <h1>Await Fulfilled Functions</h1>

    <p>An Await fulfilled function is an anonymous built-in function that is used as part of the Await specification device to deliver the promise fulfillment value to the caller as a normal completion. Each Await fulfilled function has an [[AsyncContext]] internal slot.</p>

    <p>When an Await fulfilled function _F_ is called with argument _value_, the following steps are taken:</p>

    <emu-alg>
      1. Let _asyncContext_ be _F_.[[AsyncContext]].
      1. Let _prevContext_ be the running execution context.
      1. Suspend _prevContext_.
      1. Push _asyncContext_ onto the execution context stack; _asyncContext_ is now the running execution context.
      1. Resume the suspended evaluation of _asyncContext_ using NormalCompletion(_value_) as the result of the operation that suspended it.
      1. Assert: When we reach this step, _asyncContext_ has already been removed from the execution context stack and _prevContext_ is the currently running execution context.
      1. Return *undefined*.
    </emu-alg>

    <p>The `length` property of an Await fulfilled function is 1.</p>
  </emu-clause>

  <emu-clause id="await-rejected">
    <h1>Await Rejected Functions</h1>

    <p>An Await rejected function is an anonymous built-in function that is used as part of the Await specification device to deliver the promise rejection reason to the caller as an abrupt throw completion. Each Await rejected function has an [[AsyncContext]] internal slot.</p>

    <p>When an Await rejected function _F_ is called with argument _reason_, the following steps are taken:</p>

    <emu-alg>
      1. Let _asyncContext_ be _F_.[[AsyncContext]].
      1. Let _prevContext_ be the running execution context.
      1. Suspend _prevContext_.
      1. Push _asyncContext_ onto the execution context stack; _asyncContext_ is now the running execution context.
      1. Resume the suspended evaluation of _asyncContext_ using Completion{[[Type]]: ~throw~, [[Value]]: _reason_, [[Target]]: ~empty~} as the result of the operation that suspended it.
      1. Assert: When we reach this step, _asyncContext_ has already been removed from the execution context stack and _prevContext_ is the currently running execution context.
      1. Return *undefined*.
    </emu-alg>

    <p>The `length` property of an Await rejected function is 1.</p>
  </emu-clause>
</emu-clause>
